home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 2
/
Atari Mega Archive CD - Volume 2.iso
/
minix
/
up1510b.tgz
/
up1510b
/
src
/
commands
/
atari
/
cc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-07-15
|
17KB
|
842 lines
/*
* (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the file "../Copyright".
*/
/* Driver for Minix compilers.
Written june 1987 by Ceriel J.H. Jacobs, partly derived from old
cc-driver, written by Erik Baalbergen.
This driver is mostly table-driven, the table being in the form of
some global initialized structures.
*/
#include <errno.h>
#include <signal.h>
/*
* For all program paths (so not the other files, like libraries) that
* start with "/usr/" and are executable according to access(..., 1),
* the leading "/usr" is stripped off.
*/
#define SHELL "/bin/sh"
#define AS "/usr/bin/as"
#define CEM "/usr/lib/cem"
#define CG "/usr/lib/cg"
#define PP "/usr/lib/cpp"
#define CRT "/usr/lib/crtso.o"
#define CV "/usr/lib/cv"
#define PC "/usr/lib/em_pc"
#define END "/usr/lib/end.o"
#define LD "/usr/lib/ld"
#define LIBC "/usr/lib/libc.a"
#define LIBP "/usr/lib/libp.a"
#define OPT "/usr/lib/opt"
#define PRT "/usr/lib/prtso.o"
#define LIBPREFIX "/usr/lib/lib"
#define PEM "-R/usr/lib/pem"
#define PC_ERR "-r/usr/lib/pc_errors"
/* every pass that this program knows about has associated with it
a structure, containing such information as its name, where it
resides, the flags it accepts, and the like.
*/
struct passinfo {
char *p_name; /* name of this pass */
char *p_path; /* where is it */
char *p_from; /* suffix of source (comma-separated list) */
char *p_to; /* suffix of destination */
char *p_acceptflags; /* comma separated list; format:
flag
flag*
flag=xxx
flag*=xxx[*]
where a star matches a, possibly empty,
string
*/
int p_flags;
#define INPUT 01 /* needs input file as argument */
#define OUTPUT 02 /* needs output file as argument */
#define LOADER 04 /* this pass is the loader */
#define STDIN 010 /* reads from standard input */
#define STDOUT 020 /* writes on standard output */
#define SOURCE 040 /* give source file name as last argument
(hack for pc)
*/
#define O_OUTPUT 0100 /* -o outputfile, hack for as */
#define PREPALWAYS 0200 /* always to be preprocessed */
#define PREPCOND 0400 /* preprocessed when starting with '#' */
};
#define MAXHEAD 7
#define MAXTAIL 4
#define MAXPASS 7
/* Every language handled by this program has a "compile" structure
associated with it, describing the start-suffix, how the driver for
this language is called, which passes must be called, which flags
and arguments must be passed to these passes, etc.
The language is determined by the suffix of the argument program.
However, if this suffix does not determine a language (DEFLANG),
the callname is used.
Notice that the 's' suffix does not determine a language, because
the input file could have been derived from f.i. a C-program.
So, if you use "cc x.s", the C-runtime system will be used, but if
you use "as x.s", it will not.
*/
struct compile {
char *c_suffix; /* starting suffix of this list of passes */
char *c_callname; /* affects runtime system loaded with program */
struct pass {
char *pp_name; /* name of the pass */
char *pp_head[MAXHEAD]; /* args in front of filename */
char *pp_tail[MAXTAIL]; /* args after filename */
} c_passes[MAXPASS];
int c_flags;
#define DEFLANG 010 /* this suffix determines a language */
};
struct passinfo passinfo[] = {
{ "cpp", PP, 0, "i", "I*,D*,U*", INPUT|STDOUT },
{ "cem", CEM, "i,c", "k", "R,p,w,T*,I*,D*,U*", INPUT|OUTPUT },
{ "pc", PC, "i,p", "k", "L,p,w,e,E,T*,{*", INPUT|OUTPUT|SOURCE|PREPCOND },
{ "opt", OPT, "k", "m", "LIB=L", STDIN|STDOUT },
{ "cg", CG, "m", "s", "O=p4", INPUT|OUTPUT },
{ "as", AS, "i,s", "o", "T*", INPUT|O_OUTPUT|PREPCOND },
{ "ld", LD, "o", "out", "r,s", INPUT|LOADER },
{ "cv", CV, "out", 0, "", INPUT|OUTPUT }, /* must come after loader */
{ 0}
};
struct compile passes[] = {
{ "c", "cc",
{ { "cem", {"-L"}, {0} },
{ "opt", {0}, {0} },
{ "cg", {0}, {0} },
{ "as", {"-"}, {0} },
{ "ld", {"-c", "-a0:2", "-a1:2", "-a2:2", "-a3:2", CRT},
{LIBC, END}},
{ "cv", {0}, {0} }
},
DEFLANG
},
{ "p", "pc",
{ { "pc", { PEM, PC_ERR, "-Vw2i2j2p2f8l4" }, {0} },
{ "opt", {0}, {0} },
{ "cg", {0}, {0} },
{ "as", {"-"}, {0} },
{ "ld", {"-c", "-a0:2", "-a1:2", "-a2:2", "-a3:2", PRT},
{LIBP, LIBC,
END}},
{ "cv", {0}, {0} }
},
DEFLANG
},
{ "s", "as",
{ { "as", {0}, {0}}}, 0 },
{ 0}
};
#define MAXARGC 128 /* maximum number of arguments allowed in a list */
#define USTR_SIZE 64 /* maximum length of string variable */
typedef char USTRING[USTR_SIZE];
struct arglist {
int al_argc;
char *al_argv[MAXARGC];
};
struct arglist CALLVEC[2];
int kids[] = {-1, -1};
char *o_FILE = "a.out"; /* default name for executable file */
#define init(a) ((a)->al_argc = 1)
#define cleanup(str) (str && remove(str))
#define library(nm) \
mkstr(alloc((unsigned int)strlen(nm) + strlen(LIBPREFIX) + 7), \
LIBPREFIX, nm, ".a")
char *ProgCall = 0;
int RET_CODE = 0;
char *stopsuffix;
int v_flag = 0;
int t_flag = 0;
int noexec = 0;
char *mkstr();
char *alloc();
char *process();
char *strcpy(), *strcat();
USTRING curfil;
USTRING newfil;
struct arglist SRCFILES;
struct arglist LDFILES;
struct arglist GEN_LDFILES;
struct arglist FLAGS;
char *tmpdir = "/tmp";
char tmpname[64];
struct compile *compbase;
struct pass *loader;
struct passinfo *loaderinfo;
char *source;
trapcc(sig)
int sig;
{
signal(sig, SIG_IGN);
if (kids[0] != -1) kill(kids[0], sig);
if (kids[1] != -1) kill(kids[1], sig);
cleanup(newfil);
cleanup(curfil);
exit(1);
}
main(argc, argv)
char *argv[];
{
char *str;
char **argvec;
int count;
char *file;
ProgCall = *argv++;
mkbase();
signal(SIGHUP, trapcc);
signal(SIGINT, trapcc);
signal(SIGQUIT, trapcc);
while (--argc > 0) {
if (*(str = *argv++) != '-') {
append(&SRCFILES, str);
continue;
}
switch (str[1]) {
case 'c':
stopsuffix = "o";
break;
case 'F':
break;
case 'l':
append(&SRCFILES, library(&str[2]));
break;
case 'o':
if (argc-- >= 0)
o_FILE = *argv++;
break;
case 'S':
stopsuffix = "s";
break;
case 'v':
v_flag++;
if (str[2] == 'n')
noexec = 1;
break;
case 't':
/* save temporaries */
t_flag++;
break;
case 'T':
tmpdir = &str[2];
/*FALLTHROUGH*/
default:
append(&FLAGS, str);
}
}
mktempname(tmpname);
count = SRCFILES.al_argc;
argvec = &(SRCFILES.al_argv[0]);
while (count-- > 0) {
file = *argvec++;
source = file;
if (SRCFILES.al_argc > 1) {
write(1, file, strlen(file));
write(1, ":\n", 2);
}
file = process(file, 1);
if (file && ! stopsuffix) append(&LDFILES, file);
}
clean(&SRCFILES);
/* loader ... */
if (RET_CODE == 0 && LDFILES.al_argc > 0) {
register struct passinfo *pp = passinfo;
while (!(pp->p_flags & LOADER)) pp++;
mkstr(newfil, tmpname, pp->p_to, "");
callld(&LDFILES, !((pp+1)->p_name) ? o_FILE : newfil, loader, pp);
if (RET_CODE == 0) {
register int i = GEN_LDFILES.al_argc;
while (i-- > 0) {
remove(GEN_LDFILES.al_argv[i]);
free(GEN_LDFILES.al_argv[i]);
}
if ((++pp)->p_name) {
process(newfil, 0);
}
}
}
exit(RET_CODE);
}
remove(str)
char *str;
{
if (t_flag)
return;
if (v_flag) {
prs("rm ");
prs(str);
prs("\n");
}
if (noexec)
return;
unlink(str);
}
char *
alloc(u)
unsigned u;
{
char *malloc();
register char *p = malloc(u);
if (p == 0) panic("no space\n");
return p;
}
append(al, arg)
struct arglist *al;
char *arg;
{
char *a = alloc((unsigned) (strlen(arg) + 1));
strcpy(a, arg);
if (al->al_argc >= MAXARGC)
panic("argument list overflow\n");
al->al_argv[(al->al_argc)++] = a;
}
concat(al1, al2)
struct arglist *al1, *al2;
{
register i = al2->al_argc;
register char **p = &(al1->al_argv[al1->al_argc]);
register char **q = &(al2->al_argv[0]);
if ((al1->al_argc += i) >= MAXARGC)
panic("argument list overflow\n");
while (i-- > 0)
*p++ = *q++;
}
char *
mkstr(dst, arg1, arg2, arg3)
char *dst, *arg1, *arg2, *arg3;
{
register char *p;
register char *q = dst;
p = arg1;
while (*q++ = *p++);
q--;
p = arg2;
while (*q++ = *p++);
q--;
p = arg3;
while (*q++ = *p